package com.anzia.map;

import java.util.HashMap;
import java.util.Map;

/**
 *@ClassName HashMap1_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/10 15:46
 *@Version 1.0
 **/
public class HashMap_Demo {
    public static void main(String[] args) {
        //创建集合
        HashMap<Student, String> hashMap = new HashMap<>();
        Student student1 = new Student("孙悟空", 101);
        Student student2 = new Student("沙和尚", 102);
        Student student3 = new Student("猪八戒", 103);

        //添加数据
        hashMap.put(student1,"北京");
        hashMap.put(student2,"上海");
        hashMap.put(student3,"杭州");
        hashMap.put(new Student("孙悟空", 101),"北京");
        System.out.println(hashMap.size());
        System.out.println(hashMap);

        //删除
        //hashMap.remove(student1);
        //hashMap.clear();
        //System.out.println(hashMap);

        //遍历
        //使用keySet()方法
        System.out.println("---------");
        for (Student student : hashMap.keySet()) {
            System.out.println(student + " : " + hashMap.get(student));
        }
        //使用entrySet()方法
        System.out.println("---------");
        for (Map.Entry<Student, String> entry : hashMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}
